package com.ctl.utils.jsonserializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>Title: ObjectIdSerializer</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2019</p>
 * <p>Company: www.ctl.com</p>
 *
 * @author ctl
 * @version 1.1
 * @date 2024-11-13 16:53
 */
@Slf4j
public  class ObjectIdsSerializer extends JsonSerializer<List<ObjectId>> {
    @Override
    public void serialize(List<ObjectId> value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        try {
            List<String> list =new ArrayList<>();
            value.forEach(v->list.add(v.toString()));
            gen.writeObject(list);
        } catch (Exception e) {
            log.error("ObjectIdSerializer.serialize.error",e);
        }
    }
}